package com.wlcorp.forcazamis.dao.sample.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.sample.ISubSampleAssignDAO;
import com.wlcorp.forcazamis.domain.sample.SubSampleAssign;

public class SubSampleAssignDAOImpl extends HibernateDaoSupport implements
		ISubSampleAssignDAO {
	
	public SubSampleAssign getSubSampleAssignBySubSampleAndOwner(
			SubSampleAssign subSampleAssign) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select subSampleAssign from SubSampleAssign as subSampleAssign ")
			.append("where 1 = 1 and subSampleAssign.deleteFlag=false");
		if(null != subSampleAssign.getSubSampleId()){
			queryString.append(" and subSampleAssign.subSample.subSampleId = ").append(subSampleAssign.getSubSampleId());
		}
		if(null != subSampleAssign.getOwnerId()){
			queryString.append(" and subSampleAssign.owner.depotId = ").append(subSampleAssign.getOwnerId());
		}
			
		Object obj = getHibernateTemplate().execute(new HibernateCallback(){
					public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
			
		});
		
		if(null != obj){
			return (SubSampleAssign)obj;
		}
		return null;
	}

	public void saveSubSampleAssign(SubSampleAssign subSampleAssign)
			throws DaoException {
		getHibernateTemplate().save(subSampleAssign);
	}
//
//	public List getSubSampleAssignList(final SubSampleAssign subSampleAssign) throws DaoException {
//		StringBuffer queryStr = new StringBuffer("select subSampleAssign from SubSampleAssign as subSampleAssign")
//			.append(" join subSampleAssign.subSample")
//			.append(" group by subSampleAssign.depot");
//		return (List)getHibernateTemplate().find(queryStr.toString());
//	}

}
